﻿using System.Transactions;

namespace CTP.CRM.Core.PublicMethords
{
    public static class TransactionScopeHelper
    {
        //
        // 摘要:
        //     开启事务
        //
        // 参数:
        //   scopeOption:
        //
        //   transactionOptions:
        //
        //   asyncFlowOption:
        public static TransactionScope Create(TransactionScopeOption scopeOption = TransactionScopeOption.Required, TransactionOptions transactionOptions = default(TransactionOptions), TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Enabled)
        {
            if (transactionOptions == default(TransactionOptions))
            {
                TransactionOptions transactionOptions2 = default(TransactionOptions);
                transactionOptions2.IsolationLevel = IsolationLevel.ReadCommitted;
                transactionOptions = transactionOptions2;
            }

            return new TransactionScope(scopeOption, transactionOptions, asyncFlowOption);
        }

        //
        // 摘要:
        //     提交事务
        //
        // 参数:
        //   ts:
        public static void CompleteSafely(this TransactionScope ts)
        {
            Transaction? current = Transaction.Current;
            if ((object)current != null && current.TransactionInformation.Status == TransactionStatus.Aborted)
            {
                LogHelper.Warn("事务未提交", false);
            }
            else
            {
                ts.Complete();
            }
        }
    }
}
